package com.renyi.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.renyi.http.HttpResult;
import com.renyi.http.ParamBean;
import com.renyi.model.MybatisService;
import com.renyi.util.JSONUtils;

/**
 * 
 * @author shs
 * 
 */
@Controller
public class PatientOperationController extends BaseController {

	@Autowired
	private MybatisService mybatisService;

	// 患者-关注医生
	@RequestMapping("/setInterestedDoctor")
	public void setInterestedDoctor(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String doctorID = paramMap.get("doctorID") != null ? paramMap.get("doctorID").toString() : "";
			if ("".equals(doctorID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("医生编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("doctorID", doctorID);
			long isExist = 0;
			isExist = (Long) this.mybatisService.findIbatisListCount("getInterestedDoctorID", pMap);
			if (isExist > 0) {
				result.setCode(1);
				result.setMsg("");
			} else {
				int key = (Integer) this.mybatisService.insertIbatisObject("setInterestedDoctorSQL", pMap);
				if (key > 0) {
					result.setCode(0);
					result.setMsg("");
					;// //错误消息
				} else {
					result.setCode(-1);
					result.setMsg("");
					;// //错误消息
				}
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-取消关注医生
	@RequestMapping("/cancelInterestedDoctor")
	public void cancelInterestedDoctor(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String doctorID = paramMap.get("doctorID") != null ? paramMap.get("doctorID").toString() : "";
			if ("".equals(doctorID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("医生编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("doctorID", doctorID);
			long isExist = (Long) this.mybatisService.findIbatisListCount("getInterestedDoctorID", pMap);
			if (isExist > 0) {
				int key = (Integer) this.mybatisService.deleteIbatisObject("cancelInterestedDoctorSQL", pMap);
				if (key > 0) {
					result.setCode(0);
					result.setMsg("删除关注成功");
					;// //错误消息
				} else {
					result.setCode(-1);
					result.setMsg("删除关注失败");
					;// //错误消息
				}

			} else {
				result.setCode(1);
				result.setMsg("");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 获取关注医生列表
	@RequestMapping("getInterestedDoctor")
	public void getInterestedDoctor(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getInterestedDoctorSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-获取已关闭咨询列表
	@RequestMapping("getRecordList")
	public void getRecordList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			Integer page = (int) (paramMap.get("page") != null
					? Math.floor(Float.valueOf(paramMap.get("page").toString())) : 1);
			Integer pageSize = (int) (paramMap.get("pageSize") != null
					? Math.floor(Float.valueOf(paramMap.get("pageSize").toString())) : 10);

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			Integer beginRow = (page - 1) * pageSize + 1;
			pMap.put("beginRow", beginRow);
			pMap.put("endRow", pageSize * page);
			pMap.put("uid", uid);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getRecordListSQL", pMap);
			Long count = this.mybatisService.findIbatisListCount("getRecordListCountSQL", pMap);
			count = count != null ? count : 0;
			result.setCode(0); // 0：成功
			result.setData(data);
			result.setRecordCount(count.intValue());
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-对已关闭咨询进行评价
	@RequestMapping("/setRecordAppraise")
	public void setRecordAppraise(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 登陆ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String recordID = paramMap.get("recordID") != null ? paramMap.get("recordID").toString() : "";
			String appResult = paramMap.get("appResult") != null ? paramMap.get("appResult").toString() : "1";
			String itemID = paramMap.get("itemID") != null ? paramMap.get("itemID").toString() : "";
			String appContent = paramMap.get("appContent") != null ? paramMap.get("appContent").toString() : "";
			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			if ("".equals(recordID) || "".equals(appResult) || "".equals(itemID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("recordID", recordID);
			pMap.put("appResult", appResult);
			pMap.put("itemID", itemID);
			pMap.put("appContent", appContent);

			// 0 成功 -1 失败
			List<HashMap<String, Object>> listRes = this.mybatisService.procedureMybatisList("setRecordAppraiseSQL",
					pMap);
			String resType = "";
			if (listRes.size() > 0) {
				resType = listRes.get(0).get("resType") != null ? listRes.get(0).get("resType").toString() : "0";
				if (resType.equals("0")) {
					result.setCode(0);
					result.setMsg("");
				} else {
					result.setCode(-1);
					result.setMsg("评价失败");
				}

			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}

	}

	// 患者-获取已关闭咨询详情
	@RequestMapping("getRecordDetail")
	public void getRecordDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = paramMap.get("orderID") != null ? paramMap.get("orderID").toString() : "";
			Integer page = (int) (paramMap.get("page") != null
					? Math.floor(Float.valueOf(paramMap.get("page").toString())) : 1);
			Integer pageSize = (int) (paramMap.get("pageSize") != null
					? Math.floor(Float.valueOf(paramMap.get("pageSize").toString())) : 10);

			if ("".equals(orderID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("咨询详情编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);
			Integer beginRow = (page - 1) * pageSize + 1;
			pMap.put("beginRow", beginRow);
			pMap.put("endRow", pageSize * page);

			List<HashMap<String, Object>> list = this.mybatisService.findMybatisList("getRecordDetailSQL", pMap);
			Long count = this.mybatisService.findIbatisListCount("getRecordDetailCountSQL", pMap);

			count = count != null ? count : 0;
			result.setCode(0); // 0：成功
			result.setData(list);
			result.setRecordCount(count.intValue());
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-查看当前对话是否关闭
	@RequestMapping("getRecordState")
	public void getRecordState(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String doctorQQ = paramMap.get("doctorQQ") != null ? paramMap.get("doctorQQ").toString() : "";

			if ("".equals(doctorQQ)) {
				result.setCode(-1); // 0：成功
				result.setMsg("医生QQ参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("doctorQQ", doctorQQ);

			List<HashMap<String, Object>> list = this.mybatisService.findMybatisList("getRecordStateSQL", pMap);
			if (list != null && list.size() > 0) {
				result.setData(list.get(0));
			} else {
				list = this.mybatisService.findMybatisList("getStateSQL", pMap);
				result.setData(list.get(0));
			}
			result.setCode(0); // 0：成功
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-支付前下单开启对话
	@RequestMapping("setRecordState")
	public void setRecordState(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String patientQQ = paramMap.get("patientQQ") != null ? paramMap.get("patientQQ").toString() : "";
			String doctorID = paramMap.get("doctorID") != null ? paramMap.get("doctorID").toString() : "";
			String doctorQQ = paramMap.get("doctorQQ") != null ? paramMap.get("doctorQQ").toString() : "";
			String orderID = paramMap.get("orderID") != null ? paramMap.get("orderID").toString() : "";
			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			if ("".equals(patientQQ) || "".equals(doctorID) || "".equals(doctorQQ)|| "".equals(orderID) ) {
					result.setCode(-1); // 0：成功
					result.setMsg("有参数为空");
					this.ajaxResultJson(response, result);
					return;
				}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("patientQQ", patientQQ);
			pMap.put("doctorID", doctorID);
			pMap.put("doctorQQ", doctorQQ);
			pMap.put("orderID", orderID);
//			long isExist = this.mybatisService.findIbatisListCount("getRecordStateIDSQL", pMap);
//			if (isExist < 1) {
				int key = (Integer) this.mybatisService.insertIbatisObject("setRecordStateSQL", pMap);

				if (key > 0) {
					result.setCode(0);
					result.setMsg("");
				} else {
					result.setCode(-1);
					result.setMsg("");
				}
//			} else {
//				result.setCode(1);
//				result.setMsg("上次服务已支付，还未关闭，不用再开通");
//			}
			
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

}
